<template>
  <div class="app-container">

    <div class="filter-container">
      <el-button v-waves class="filter-item" type="primary" @click="addCate()">
        <i class="el-icon-plus" />&nbsp;&nbsp;添加分类
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center">
        <template slot-scope="scope">
          <span v-html="scope.row.title_show" />
        </template>
      </el-table-column>

      <el-table-column v-permission="['/article/updateCate','/article/deleteCate']" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <div>
            <span v-permission="['/article/updateCate']">
              <el-button size="small" type="primary" @click="updateCate(scope.row)">编辑</el-button>
            </span>
            <span v-permission="['/article/deleteCate']">
              <el-button size="small" type="primary" @click="confirmDelete(scope.row)">删除</el-button>
            </span>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加分类" :visible.sync="cateVisible">
      <el-form>
        <el-form-item label="选择分类">
          <el-select v-model="pid" placeholder="上级分类">
            <el-option label="顶级分类" value="0" />
            <el-option v-for="(item,index) in list" :key="index" :label="item.title" :value="item.id" v-html="item.title_show" />
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称" label-width="70px">
          <el-input v-model="name" />
        </el-form-item>

        <el-alert
          title="注：分类最多可以选择两级分类"
          type="error"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleAdd">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑分类" :visible.sync="cateUpdateVisible">
      <el-form>
        <el-form-item label="选择分类">
          <el-select v-model="pid" placeholder="上级分类">
            <el-option label="顶级分类" value="0" />
            <el-option v-for="(item,index) in list" :key="index" :label="item.title" :value="item.id" v-html="item.title_show" />
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称" label-width="70px">
          <el-input v-model="name" />
        </el-form-item>

        <el-alert
          title="注：分类最多可以选择两级分类"
          type="error"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getCate, addCate, updateCate, deleteCate } from '@/api/article.js'
import waves from '@/directive/waves'
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
import permission from '@/directive/permission/index.js'

export default {
  components: { Pagination },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      list: null,
      listLoading: true,

      name: null,
      pid: '0',
      cateVisible: false,

      cateUpdateVisible: false,
      id: -1

    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getCate(this.listQuery).then(res => {
        if (res.code == 20000) {
          this.list = res.data
          setTimeout(() => {
            this.listLoading = false
          }, 0.3 * 1000)
        } else {
          this.listLoading = true
        }
      })
    },

    // 添加分类
    addCate() {
      this.cateVisible = true
    },

    // 提交增加分类
    handleAdd() {
      addCate({ pid: this.pid, name: this.name }).then(res => {
        if (res.code == 20000) {
          this.cateVisible = false
          this.getList()
        }
      })
    },

    // 更新分类
    updateCate(row) {
      if (row.pid != 0) {
        this.pid = Number(row.pid)
      } else {
        this.pid = '0'
      }
      this.name = row.title
      this.id = row.id
      this.cateUpdateVisible = true
    },

    // 提交编辑分类
    handleEdit() {
      const param = { id: this.id, pid: this.pid, name: this.name }
      console.log(param)
      updateCate(param).then(res => {
        if (res.code == 20000) {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.cateUpdateVisible = false
          this.getList()
        }
      })
    },

    // 删除分类
    confirmDelete(row) {
      this.$confirm('确认删除该分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCate(row.id).then(res => {
          this.getList()
          this.$message({
            message: '分类已删除',
            type: 'success'
          })
        })
      }).catch(() => {

      })
    }

  }
}
</script>

